'use strict';
const dayjs = require("dayjs");
module.exports = app => {
    const { INTEGER, DATE,STRING } = app.Sequelize;
    const Recharge = app.model.define(
      'Recharge',
          {
              id: {type: INTEGER, primaryKey: true, autoIncrement: true},
              user_id: INTEGER,
              type: INTEGER, //  '-1' COMMENT '0 消费 1 充值 2手动扣款',
              amount: {type: 'DECIMAL(10,2)', defaultValue: 0.00},
              // 剩余余额
              balance: {type: 'DECIMAL(10,2)', defaultValue: 0.00},
              post_join_id: INTEGER,
              desc: STRING(255),
              create_time: {
                  type: DATE,
                  get() {
                      return dayjs(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
                  }
              },
              updated_time: {
                  type: DATE,
                  get() {
                      return dayjs(this.getDataValue('updated_time')).format('YYYY-MM-DD HH:mm:ss');
                  }
              }
          },
          {
              tableName: 'recharge',
              timestamps: true, // 启用时间戳(createdAt, updatedAt)
              updatedAt: 'updated_time', // 不想要 updatedAt
              createdAt: 'create_time', // 想要  createdAt 但是希望名称叫做 create_time
          }
    );
    Recharge.associate = function() {
        // 用户
        app.model.Recharge.belongsTo(app.model.User, { foreignKey: 'user_id', targetKey: 'id', as: 'user' });
    }
    return Recharge;
};

